package rapier.sso.server.config.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import rapier.sso.common.utils.ThreadLocalUtil;
import rapier.sso.core.entity.TDbSsoAppEntity;
import rapier.sso.server.constant.RapierComConstant;
import rapier.sso.server.service.RapierAppsService;

public class RapierWebInterceptor implements HandlerInterceptor {

	private RapierAppsService appsService;

	public RapierWebInterceptor(RapierAppsService appsService) {
		this.appsService = appsService;
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		String appId = request.getParameter(RapierComConstant.RAPIER_APP_ID);
		if (StringUtils.isNotBlank(appId)) {
			TDbSsoAppEntity appInfo = appsService.getAppAuthInfo(Long.valueOf(appId));
			ThreadLocalUtil.put(RapierComConstant.RAPIER_APP, appInfo);
		}
		return true;
	}
}
